/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.data.Item;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import issr.boundary.vaadin.AbstractView;
import issr.controller.ControlloreGestioneDipendenteAmministratoreG;
import issr.entity.Utente;
import issr.misc.Labels;
import java.util.ArrayList;

/**
 *
 * @author Soulreaver
 */
public class PannelloGestioneDipendenteAmministratoreG extends AbstractView {

    private ControlloreGestioneDipendenteAmministratoreG controllore;
    private final Table textTable;
    private final VerticalLayout layout;

    public PannelloGestioneDipendenteAmministratoreG() {
        controllore = new ControlloreGestioneDipendenteAmministratoreG();

        layout = new VerticalLayout();
        textTable = new Table();
        textTable.addContainerProperty(Labels.NOME, String.class, null);
        textTable.addContainerProperty(Labels.COGNOME, String.class, null);
        textTable.addContainerProperty(Labels.RUOLO, String.class, null);
        textTable.addContainerProperty(Labels.PUNTO_VENDITA, String.class, null);
        // Create the visual components of the view.
        setSizeFull();
        setCompositionRoot(layout);

        layout.setHeight("100%");
        layout.setWidth("100%");
        layout.setSpacing(true);
        layout.setMargin(true);

        layout.addComponent(textTable);
        // Get the log container and add it to the view.

    }

    public void visualizzaListaDipendenti() {
        ArrayList<Utente> dipendenti = controllore.ottieniListaDipendenti();

        // For each line in the log, add a container item.
        for (int dummyId = 0; dummyId < dipendenti.size(); dummyId++) {
            Item item = textTable.addItem(dummyId);
            item.getItemProperty(Labels.NOME).setValue(dipendenti.get(dummyId).getNome());
            item.getItemProperty(Labels.COGNOME).setValue(dipendenti.get(dummyId).getCognome());
            item.getItemProperty(Labels.RUOLO).setValue(dipendenti.get(dummyId).getRuolo().getNome());
            if (dipendenti.get(dummyId).getPuntovendita() != null) {
                item.getItemProperty(Labels.PUNTO_VENDITA).setValue(dipendenti.get(dummyId).getPuntovendita().getNome());
            }
        }

        textTable.sort(new Object[]{Labels.RUOLO}, new boolean[]{true});
        textTable.setSizeFull();
        textTable.setPageLength(0);
        textTable.setImmediate(true);
    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
        textTable.removeAllItems();
        visualizzaListaDipendenti();
    }
}
